"""
数据库封装模块
"""
import pymysql


class DB:
    def __init__(self, db_config: dict):
        # 创建连接
        self.conn = pymysql.connect(**db_config)

    def get_one(self, sql):
        """
        获取一条数据
        :param sql:
        :return:
        """
        with self.conn.cursor() as cursor:
            cursor.execute(sql)
            return cursor.fetchone()

    def get_many(self, sql, size: int):
        """
        获取多条数据
        :param sql:
        :return:
        """
        with self.conn.cursor() as cursor:
            cursor.execute(sql)
            return cursor.fetchone(size)

    def get_all(self, sql):
        """
        获取所有数据
        :param sql:
        :return:
        """
        with self.conn.cursor() as cursor:
            cursor.execute(sql)
            return cursor.fetchall()

    def exist(self, sql):
        """
        是否存在数据
        :param sql:
        :return:
        """
        with self.conn.cursor() as cursor:
            cursor.execute(sql)
            if cursor.fetchone():
                return True
            else:
                return False

    def __del__(self):
        """
        忘记关闭连接的时候关闭连接
        :return:
        """
        self.conn.close()
        print('连接被关闭了')


if __name__ == '__main__':
    import settings

    db = DB(settings.DB_CONFIG)
    # 获取一条数据
    sql = ''
    res = db.get_one(sql)
